# -*- coding:utf-8 -*-
# !/usr/bin/python3

import os
import uuid
import codecs
import chardet
from mysqlpool import OPMysql



def Test2(rootDir):
    list_dirs = os.walk(rootDir)

    for root, dirs, files in list_dirs:
        for f in files:
            if str(f).endswith('txt'):
                opMysql = OPMysql()
                path = os.path.join(root, f)
                dealPath = "/".join(path.split("\\"))
                sql = buildInsertSql(dealPath, getFileContent(path), os.path.getsize(path))
                getSql = buildGetSql(dealPath)
                count = opMysql.op_select(getSql)
                if not count['num']:
                    try:
                        opMysql.op_insert(sql)
                    except Exception:
                        print(sql)
                opMysql.dispose()


def buildInsertSql(path, content, size: int):
    uid = str(uuid.uuid4())
    suid = ''.join(uid.split('-'))
    return 'INSERT INTO data_novel(`novel_id`, `path`, `content`, `rec_status`, `create_date`, `from`, `size`) VALUES ("{}", "{}", "{}", 1, now(), "files", {})'.format(
        suid, path, content, size / 1024)


def getFileContent(filepath):
    # 解码utf-16-LE
    try:
        coding = getFileEncoding(filepath)
        print(coding)
        if coding == 'GB2312':
            coding = 'gb18030'
        my_file = codecs.open(filepath, 'r', encoding=coding)
        content = my_file.read()
        return content.replace("\"", "`")
    except UnicodeDecodeError:
        return filepath + '------读取文件出错了。。。'
    finally:
        my_file.close()


def getFileEncoding(filepath):
    # 二进制方式读取，获取字节数据，检测类型
    try:
        file = open(filepath, 'rb')
        data = file.read()
        return chardet.detect(data)['encoding']
    finally:
        file.close()


def buildGetSql(path):
    return 'select count(1) as num from data_novel where path = "{}"'.format(path)


if __name__ == "__main__":
    Test2("G:\\娱乐\\insert")
